#![allow(dead_code)]

use crate::model::enum_type::*;
use serde::{Deserialize, Serialize};

/// 版本信息
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct VersionItem<'a> {
    /// 名称
    name: &'a str,
    /// 版本号
    version: &'a str,
    /// 架构
    arch: ArchType,
    /// 系统
    os: OsType,
    /// 描述
    description: &'a str,
    /// 下载地址
    url: &'a str,
    /// 图标
    icon: &'a str,
    /// 大小
    size: u64,
    /// 包类型
    pack_type: PackType,
    /// 是否是lts版本
    lts: bool,
    /// 标签
    tags: Vec<&'a str>,
    /// 校验和
    sum: &'a str,
    /// 校验和类型
    sum_type: SumType,
    /// 额外信息
    extra: Option<&'a str>,
    /// 是否存在
    exist: bool,
    /// 频道
    channel: Option<&'a str>,
}

impl<'a> Default for VersionItem<'a> {
    fn default() -> Self {
        VersionItem {
            name: "",
            version: "",
            arch: ArchType::Unknown,
            os: OsType::Unix,
            description: "",
            url: "",
            icon: "",
            size: 0,
            pack_type: PackType::Unarchived,
            lts: false,
            tags: vec![],
            sum: "",
            sum_type: SumType::MD5,
            extra: None,
            exist: false,
            channel: None,
        }
    }
}

impl<'a> VersionItem<'a> {
    pub fn new(
        name: &'a str,
        version: &'a str,
        arch: ArchType,
        os: OsType,
        description: &'a str,
        url: &'a str,
        icon: &'a str,
        size: u64,
        pack_type: PackType,
        lts: bool,
        tags: Vec<&'a str>,
        sum: &'a str,
        sum_type: SumType,
        extra: Option<&'a str>,
        exist: bool,
        channel: Option<&'a str>,
    ) -> Self {
        Self {
            name,
            version,
            arch,
            os,
            description,
            url,
            icon,
            size,
            pack_type,
            lts,
            tags,
            sum,
            sum_type,
            extra,
            exist,
            channel,
        }
    }
    pub fn name(&self) -> &'a str {
        self.name
    }

    pub fn version(&self) -> &'a str {
        self.version
    }

    pub fn arch(&self) -> &ArchType {
        &self.arch
    }

    pub fn os(&self) -> &OsType {
        &self.os
    }

    pub fn description(&self) -> &'a str {
        self.description
    }

    pub fn url(&self) -> &'a str {
        self.url
    }

    pub fn icon(&self) -> &'a str {
        self.icon
    }

    pub fn size(&self) -> u64 {
        self.size
    }

    pub fn tags(&self) -> &Vec<&'a str> {
        &self.tags
    }

    pub fn sum(&self) -> &'a str {
        self.sum
    }

    pub fn extra(&self) -> Option<&'a str> {
        self.extra
    }

    pub fn pack_type(&self) -> &PackType {
        &self.pack_type
    }

    pub fn lts(&self) -> bool {
        self.lts
    }

    pub fn sum_type(&self) -> &SumType {
        &self.sum_type
    }

    pub fn exist(&self) -> bool {
        self.exist
    }
    pub fn channel(&self) -> Option<&'a str> {
        self.channel
    }
    pub fn to_string(&self) -> String {
        format!(
            "{} {} {} {} {} {} {} {} {} {} {} {} {} {:?}",
            self.name,
            self.version,
            self.arch,
            self.os,
            self.description,
            self.url,
            self.icon,
            self.size,
            self.pack_type,
            self.lts,
            self.tags
                .iter()
                .map(|x| x.to_string())
                .collect::<Vec<_>>()
                .join(","),
            self.sum,
            self.sum_type,
            self.extra
        )
    }
}
